﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Xml.Linq;

namespace BoolanElement
{
    public class BooElementTransform
    {

        public BooElementTransform()
        {
            Rotate = new RotateTransform();
            Scale = new ScaleTransform();
            Translate = new TranslateTransform();
            Margin = new Thickness(0, 0, 0, 0);
        }
        private StringBuilder _ZIndex = new StringBuilder("-1");
        public int ZIndex
        {
            get 
            {
                return Convert.ToInt32(_ZIndex.ToString());
            }
            set
            {
                _ZIndex.Clear();
                _ZIndex.Append(value.ToString());
            }
        }
  
        public Thickness Margin
        {
            get;
            set;
        }
        public RotateTransform Rotate
        {
            get;
            set;
        }
        public ScaleTransform Scale
        {
            get;
            set;
        }
        public TranslateTransform Translate
        {
            get;
            set;
        }
        public StringBuilder _Width = new StringBuilder("0");
        public double Width
        {
            get
            {
                return Convert.ToDouble(_Width.ToString());
            }
            set
            {
                _Width.Clear();
                _Width.Append(value.ToString());
                //UpdateWidth(_Width);
            }
        }

        public StringBuilder _Height = new StringBuilder("0");
        public double Height
        {
            get
            {
                return Convert.ToDouble(_Height.ToString());
            }
            set
            {
                _Height.Clear();
                _Height.Append(value.ToString());
                //UpdateHeight(_Height);
            }
        }

        public XElement Save()
        {
            XElement ret = new XElement("Transform");
            ret.SetAttributeValue("ZIndex",_ZIndex.ToString());

            XElement Rotate = new XElement("Rotate");
            XElement xmlRotateAngle = new XElement("RotateAngle");
            xmlRotateAngle.Value = this.Rotate.Angle.ToString();
            XElement xmlRotateCentreX = new XElement("RotateCentreX");
            xmlRotateCentreX.Value = this.Rotate.CenterX.ToString();
            XElement xmlRotateCentreY = new XElement("RotateCentreY");
            xmlRotateCentreY.Value = this.Rotate.CenterY.ToString();
            XElement Scale = new XElement("Scale");
            XElement xmlScaleX = new XElement("ScaleX");
            xmlScaleX.Value = this.Scale.ScaleX.ToString();
            XElement xmlScaleY = new XElement("ScaleY");
            xmlScaleY.Value = this.Scale.ScaleY.ToString();
            XElement xmlScaleCentreX =new XElement("ScaleCentreX");
            xmlScaleCentreX.Value = this.Scale.CenterX.ToString();
            XElement xmlScaleCentreY = new XElement("ScaleCentreY");
            xmlScaleCentreY.Value = this.Scale.CenterY.ToString();
            XElement Translate = new XElement("Translate");
            XElement xmlTranslateX = new XElement("TranslateX");
            xmlTranslateX.Value = this.Translate.X.ToString();
            XElement xmlTranslateY = new XElement("TranslateY");
            xmlTranslateY.Value = this.Translate.Y.ToString();
            XElement Margin = new XElement("Margin");
            XElement xmlMarginLeft = new XElement("MarginLeft");
            xmlMarginLeft.Value = this.Margin.Left.ToString();
            XElement xmlMarginTop =new XElement("MarginTop");
            xmlMarginTop.Value = this.Margin.Top.ToString();
            XElement Width = new XElement("Width");
            Width.Value = this.Width.ToString();
            XElement Height = new XElement("Height");
            Height.Value = this.Height.ToString();

            ret.Add(Rotate);
            Rotate.Add(xmlRotateAngle);
            Rotate.Add(xmlRotateCentreX);
            Rotate.Add(xmlRotateCentreY);
            ret.Add(Scale);
            Scale.Add(xmlScaleX);
            Scale.Add(xmlScaleY);
            Scale.Add(xmlScaleCentreX);
            Scale.Add(xmlScaleCentreY);
            ret.Add(Translate);
            Translate.Add(xmlTranslateX);
            Translate.Add(xmlTranslateY);
            ret.Add(Margin);
            Margin.Add(xmlMarginLeft);
            Margin.Add(xmlMarginTop);
            ret.Add(Width);
            ret.Add(Height);

            return ret;
        }

        public void Load(XElement root)
        {
            if (root.Attribute("ZIndex") != null)
                ZIndex = Convert.ToInt32(root.Attribute("ZIndex").Value);
            XElement Rotate = root.Element("Rotate");
            XElement Scale = root.Element("Scale");
            XElement Translate = root.Element("Translate");
            XElement Margin = root.Element("Margin");
            XElement Width = root.Element("Width");
            XElement Height = root.Element("Height");

            this.Rotate.Angle = Convert.ToDouble(Rotate.Element("RotateAngle").Value);
            this.Rotate.CenterX = Convert.ToDouble(Rotate.Element("RotateCentreX").Value);
            this.Rotate.CenterY = Convert.ToDouble(Rotate.Element("RotateCentreY").Value);

            this.Scale.ScaleX = Convert.ToDouble(Scale.Element("ScaleX").Value);
            this.Scale.ScaleY = Convert.ToDouble(Scale.Element("ScaleX").Value);
            this.Scale.CenterX = Convert.ToDouble(Scale.Element("ScaleCentreX").Value);
            this.Scale.CenterY = Convert.ToDouble(Scale.Element("ScaleCentreY").Value);

            this.Translate.X = Convert.ToDouble(Translate.Element("TranslateX").Value);
            this.Translate.Y = Convert.ToDouble(Translate.Element("TranslateY").Value);
        
            double MarginLeft = Convert.ToDouble(Margin.Element("MarginLeft").Value);
            double MarginTop = Convert.ToDouble(Margin.Element("MarginTop").Value);
            this.Margin = new Thickness(MarginLeft, MarginTop, 0, 0);

         
            this.Width = Convert.ToDouble(Width.Value);
            this.Height = Convert.ToDouble(Height.Value);
        }

        public BooElementTransform Clone()
        {
            BooElementTransform elementTransform = (BooElementTransform)this.MemberwiseClone();

            elementTransform._Width = new StringBuilder("0");
            elementTransform._Height = new StringBuilder("0");
            elementTransform.Width = Width;
            elementTransform.Height = Height;

            elementTransform.Translate = new TranslateTransform();
            elementTransform.Translate.X = Translate.X;
            elementTransform.Translate.Y = Translate.Y;

            elementTransform.Rotate = new RotateTransform();
            elementTransform.Rotate.CenterX = Rotate.CenterX;
            elementTransform.Rotate.CenterY = Rotate.CenterY;
            elementTransform.Rotate.Angle = Rotate.Angle;

            elementTransform.Scale = new ScaleTransform();
            elementTransform.Scale.CenterX = Scale.CenterX;
            elementTransform.Scale.CenterY = Scale.CenterY;
            elementTransform.Scale.ScaleX = Scale.ScaleX;
            elementTransform.Scale.ScaleY = Scale.ScaleY;

            return elementTransform;
        }
    }
}
